Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD Custom color scheme API #839

Merged
merged 10 commits into from
Aug 2, 2019
Merged

ADD Custom color scheme API #839

merged 10 commits into from
Aug 2, 2019

Conversation

hainm
Copy link
Collaborator

@hainm hainm commented Aug 1, 2019

A live version here: https://nbviewer.jupyter.org/github/arose/nglview/blob/644fa66a8461b52c01a5e4df4d2a99486b690fea/notebooks/custom_color.ipynb
(might need to Zoom Out to see the full page: #840 )

Screen Shot 2019-08-01 at 11 56 39 PM

Code

Selection Scheme

import nglview as nv
from nglview.color import ColormakerRegistry

cm = ColormakerRegistry
cm.add_selection_scheme("awesome",
        [['blue', '1-10']])
_ColormakerRegistry()
v0 = nv.demo(default=False)
v0.center()
v0.add_cartoon(color='awesome')
v0
NGLWidget()
assert v0._ngl_repr_dict['0']['0']['params']['colorScheme'] == 'awesome'

Function

cm.add_scheme_func('bla_bla','''
 this.atomColor = function (atom) {
     if (atom.serial < 1000) {
       return 0x0000FF // blue
     } else if (atom.serial > 2000) {
       return 0xFF0000 // red
     } else {
       return 0x00FF00 // green
     }
 }
''')
v1 = nv.demo(default=False)
v1.center()
v1.add_cartoon(color='bla_bla')
v1
NGLWidget()
# nv.write_html('index.html', [v0, v1])

# !open index.html
# !google-chrome index.html

# NOTE: If using master branch, make sure to copy nglview/static/index.js to <this folder>/nglview-js-widgets.js

@sperezconesa
Copy link

Hello,
Could you give an example function in which a color scale and not discrete colors are used? Thank you!
Best,
Sergio

@hainm
Copy link
Collaborator Author

hainm commented Sep 5, 2020

@sperezconesa I personally don't know "how". @arose Do you have any suggestion? thanks.

@seanrjohnson
Copy link

Hi,

I can't get this example to work in Colab (works in local notebooks though).

Is there a way to do custom coloring by residue in colab?

Here is the code I tried.

Output from colab was a completely white structure
Output from local notebook was as shown above, with the blue section.

Cell 1

!pip install -q nglview pytraj
from google.colab import output
output.enable_custom_widget_manager()

Cell 2

import nglview as nv
from nglview.color import ColormakerRegistry

cm = ColormakerRegistry
cm.add_selection_scheme("awesome",
        [['blue', '1-10']])

v0 = nv.demo(default=False)
v0.center()
v0.add_cartoon(color='awesome')
v0

@hainm
Copy link
Collaborator Author

hainm commented Jul 8, 2022

Hi,

I can't get this example to work in Colab (works in local notebooks though).

Is there a way to do custom coloring by residue in colab?

Here is the code I tried.

Output from colab was a completely white structure Output from local notebook was as shown above, with the blue section.

Cell 1

!pip install -q nglview pytraj
from google.colab import output
output.enable_custom_widget_manager()

Cell 2

import nglview as nv
from nglview.color import ColormakerRegistry

cm = ColormakerRegistry
cm.add_selection_scheme("awesome",
        [['blue', '1-10']])

v0 = nv.demo(default=False)
v0.center()
v0.add_cartoon(color='awesome')
v0

hi @seanrjohnson, there is nothing we can do about colab besides waiting for google to improve it.

@wangm23456
Copy link
Contributor

This seems to be the only effective way at present.

scheme = nv.color._ColorScheme([['red', '1-6'], ['yellow', '20-30']],

@mretier
Copy link

mretier commented Nov 29, 2023

Is this also supported for surfaces? I can't seem to get it to work. The surface gets created, but it is all white. It does work for the cartoon representation.

from nglview.color import ColormakerRegistry
cm = ColormakerRegistry
cm.add_selection_scheme('awesome', [['red', '1-10'], ['yellow', '222-300']])
view.add_representation('surface', component=2, color='awesome')

@hainm
Copy link
Collaborator Author

hainm commented Dec 4, 2023

@mretier would you mind ask something like 'is is possible to custom color of the surface repr or not?', in NGL repo: https://github.com/nglviewer/ngl

cheers.

@mretier
Copy link

mretier commented Dec 7, 2023

@hainm I asked them here. I'll keep you posted.

@hainm
Copy link
Collaborator Author

hainm commented Dec 7, 2023

thanks @mretier

@mretier
Copy link

mretier commented Dec 7, 2023

@hainm please check out the response in the above mentioned thread. It appears to be possible.

@hainm
Copy link
Collaborator Author

hainm commented Dec 7, 2023

Is this also supported for surfaces? I can't seem to get it to work. The surface gets created, but it is all white. It does work for the cartoon representation.

from nglview.color import ColormakerRegistry
cm = ColormakerRegistry
cm.add_selection_scheme('awesome', [['red', '1-10'], ['yellow', '222-300']])
view.add_representation('surface', component=2, color='awesome')

@mretier You are using component=2 here, is that your intention?

@mretier
Copy link

mretier commented Dec 7, 2023

@hainm yes, it was my intention in this specific case. When using the same code to apply to the cartoon representation it worked

@hainm
Copy link
Collaborator Author

hainm commented Dec 7, 2023

@mretier I've made a comment here, is that similar to what you're trying? nglviewer/ngl#1005 (comment)

@mretier
Copy link

mretier commented Dec 10, 2023

@hainm thanks for the comment. I was able to reproduce your results. Howevever, it does not seem to work with my own code that's a bit different. It's not clear to me why it wouldn't work in my case:

import nglview as nv
from nglview.color import ColormakerRegistry
from Bio.PDB import *
# new cell
parser = PDBParser()
# load Zymomonas mobilis alcohol dehydrogenase
Zmadh = parser.get_structure('3OWO', './3owo_Zmadh.pdb')
# remove second dimer structure (PDB file stores two dimer copies)
Zmadh[0].detach_child('C')
Zmadh[0].detach_child('D')
# new cell
cm = ColormakerRegistry
cm.add_selection_scheme('awesome', [['red', '1-10'], ['yellow', '222-300']])
# new cell
# display only a monomer of the dimer
view = nv.NGLWidget()
view.add_structure(nv.BiopythonStructure(Zmadh[0]['A']))
view.add_representation('surface', color='awesome')
view

@hainm
Copy link
Collaborator Author

hainm commented Dec 10, 2023 via email

@mretier
Copy link

mretier commented Dec 15, 2023

@hainm could you elaborate what exactly you mean? How would if find out if my selection was incorrect?

@hainm
Copy link
Collaborator Author

hainm commented Dec 16, 2023 via email

@mretier
Copy link

mretier commented Dec 19, 2023

@hainm thanks for your effort, the above code should be fully functional. For your convenience I attached the full code here as well as the .pdb file.
code and structure.zip

@hainm
Copy link
Collaborator Author

hainm commented Dec 19, 2023

I could reproduce the issue with your code + file. Let me play more with it. Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants